package system.controller;

import com.jfinal.aop.Enhancer;
import com.jfinal.core.Controller;
import com.jfinal.json.Json;
import com.jfinal.kit.JsonKit;
import core.jfinal.ext.autoroute.ControllerBind;
import core.toolbox.Period.PeriodUtils;
import system.service.UserSubjectBalanceService;

import javax.swing.text.EditorKit;
import java.util.List;

@ControllerBind(controllerKey = "subjectBalance", viewPath = "")
public class SubjectBalanceController extends Controller {

    UserSubjectBalanceService userSubjectBalanceService = Enhancer.enhance(UserSubjectBalanceService.class);


    /**
     * 获取科目余额表
     */
    public void get(){
        String startStr = getPara("start")==null?String.valueOf(PeriodUtils.getCurrentPeriod()):getPara("start");
        String endStr = getPara("end")==null?String.valueOf(PeriodUtils.getCurrentPeriod()):getPara("end");

        int start = Integer.valueOf(startStr);
        int end = Integer.valueOf(endStr);

        List subjectBalance = userSubjectBalanceService.getSubjectBalanceDuringPeriod(start, end);
        setAttr("subjectBalance", subjectBalance);

        setAttr("selectPeriod", PeriodUtils.getSelectPeriod(start, end));

        setAttr("currentSelectPeriod", getCurrentSelectPeriod(start, end));

        render("/page/TrialBalance.ftl");

    }

    private String getCurrentSelectPeriod(int start, int end){
        if(start < end){
            return "2017年第"+start+"期至2017年第"+end+"期";
        }else if(start == end) {
            return "2017第"+start+"期";
        }else{
            return "2017年第"+end+"期至2017年第"+start+"期";
        }
    }

}
